(() => {
    let ele = {};

    ele.options = $('#tab-team li');
    ele.options2 = $('#tab-two button');

    getUserInfo()

    // 领取成功
    ele.modal_successfully = $('#modal-successfully')
    ele.confirm_successfully = $('#confirm-successfully')
    ele.successMoney = $('#successMoney')
    // 领取成功
    ele.confirm_successfully.onclick = () => {
        ele.modal_successfully.style.display = 'none'
    }

    // 恭喜您开出
    ele.modal_open = $('#modal-open')
    ele.openMoney = $('#openMoney')
    ele.confirm_open = $('#confirm-open')
    // 恭喜您开出
    ele.confirm_open.onclick = () => {
        ele.modal_open.style.display = 'none'
    }

    // 新人盲盒开盒概率
    ele.modal_new = $('#modal-new')
    ele.confirm_new = $('#confirm-new')
    ele.confirm_new.onclick = () => {
        ele.modal_new.style.display = 'none'
    }

    // 普通盲盒开盒概率
    ele.modal_ordinary = $('#modal-ordinary')
    ele.confirm_ordinary = $('#confirm-ordinary')
    ele.confirm_ordinary.onclick = () => {
        ele.modal_ordinary.style.display = 'none'
    }

    // VIP盲盒开盒概率
    ele.modal_VIP = $('#modal-VIP')
    ele.confirm_VIP = $('#confirm-VIP')
    ele.confirm_VIP.onclick = () => {
        ele.modal_VIP.style.display = 'none'
    }

    const typeList = ['', 'one', 'two', 'three'];
    const typeList2 = ['', 'gift', 'blind'];

    let typeName = 'one';
    let typeName2 = 'blind';
    let currentPage = {
        one: 1,
        two: 2,
        three: 3,
        gift: 4,
        blind: 5
    }
    getList(typeList.indexOf(typeName));
    tabBar();

    // 选项卡选项功能
    function tabBar() {
        for (let i = 0; i < ele.options.length; i++) {
            ele.options[i].addEventListener('click', e => {
                // let cls = e.target.className;
                let cls = e.target.parentElement.className;
                typeName = cls.split(' ')[0].split('-')[1];
                let typeIndex = typeList.indexOf(typeName);
                if (currentPage[typeName] == 1) {
                    // 礼盒商店
                    getList(typeIndex);
                } else if (currentPage[typeName] == 2) {
                    // 盲盒商店
                    getList(typeIndex);
                } else if (currentPage[typeName] == 3) {
                    // 已获得
                    tabBar2()
                    getObtained(typeList2.indexOf(typeName2))
                }
            })
        }
    }
    function getList(type) {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.STORELIST,
            method: 'GET',
            header: {
                Authorization: token
            },
            data: {
                type: type
            },
            success: res => {
                if (res.code == 1) drawPropList(res.data);
                else openTips(res.msg);
            }
        });
    }
    // 渲染新人任务列表
    function drawPropList(data) {
        let html = '';
        let elm = $('#list-' + typeName);
        for (let i = 0; i < data.length; i++) {
            let lv = data[i];
            let box = ''
            if (lv.type == 1) {
                box = '<div class="giftbox">' +
                    '<div class="giftimg">' +
                    '<img src="http://img.qunx.top/xianwang/lihe/' + lv.id + '.png" alt="">' +
                    '</div>' +
                    '<div class="giftinfo">' +
                    '<div class="gifttitle">' +
                    '<span class="a1">' + lv.title + '</span>' +
                    '<span class="a2">售价：' + lv.price + '灵石</span>' +
                    '</div>' +
                    '<div class="gifttime">' +
                    '<div class="giftc">' +
                    '<p class="b1">连续' + lv.num + '天每天可领<span class="orgorn">' + lv.prize_gift + '米</span>' + '零钱</p>' +
                    '<p class="b1">(合计' + (lv.num * lv.prize_gift) + '米)</p>' +
                    '</div>' +
                    '<div class="sale" onclick=submits(' + lv.id + ',"' + lv.price + '")>' +
                    '<span class="salen">购买' + '</span>' +
                    '</div>' +
                    '</div>' +
                    '</div>' +
                    '</div>'
            } else {
                let sp = ''
                let txt = ''
                if (lv.price == 0) {
                    sp = '<p class="b1">' + '<span class="orgorn">完成新人任务免费送</span>' + '</p>'
                    txt = '免费获取'
                } else {
                    sp = '<p class="b1">' + '<span class="orgorn">售价：' + lv.price + '灵石</span>' + '</p>'
                    txt = '购买'
                }

                box = '<div class="giftbox">' +
                    '<div class="giftimg">' +
                    '<img src="http://img.qunx.top/xianwang/maghe/' + (i + 1) + '.png" alt="">' +
                    '</div>' +
                    '<div class="giftinfo">' +
                    '<p class="probability" onclick=opentan(' + lv.id + ')>查看概率' + '</p>' +
                    '<span class="a1">' + lv.title + '</span>' +
                    '<p class="b1">可随机开出' + '<span class="orgorn">' + parseInt(lv.prize_min) + '-' + parseInt(lv.prize_max) + '米</span>的零钱' + '</p>' +
                    '<div class="gifttime">' +
                    '<div class="giftc">' +
                    sp +
                    '</div>' +
                    '<div class="sale" onclick=submit2s(' + lv.id + ',"' + lv.price + '")>' +
                    '<span class="salen">' + txt + '</span>' +
                    '</div>' +
                    '</div>' +
                    '</div>' +
                    '</div>'
            }
            html += box
        }
        elm.innerHTML = html;
    }
    // 购买礼盒
    window.submits = submits
    function submits(id, num) {
        let money = getCookie('stone')//灵石
        let is_vip = getCookie('is_vip')//是否为VIP
        if (id == 1 && parseInt(money) >= parseInt(num)) {
            buy(id)
        } else if (id == 1 && parseInt(money) < parseInt(num)) {
            openTips('灵石数量不足')
        } else if (id != 1 && is_vip == 1) {
            buy(id)
        } else if (id != 1 && is_vip == 0) {
            openTips('升级到VIP才能购买哦')
        }
    }
    // 购买盲盒页面 查看概率提示框
    window.opentan = opentan
    function opentan(id) {
        if (id == 8) {
            ele.modal_new.style.display = 'block'
        } else if (id == 9) {
            ele.modal_ordinary.style.display = 'block'
        } else if (id == 10) {
            ele.modal_VIP.style.display = 'block'
        }
    }
    // 购买盲盒
    window.submit2s = submit2s
    function submit2s(id, num) {
        let is_vip = getCookie('is_vip')//是否为VIP
        if (id == 8 || id == 9) {
            buy(id)
        } else if (id == 10 && is_vip == 1) {
            buy(id)
        } else if (id == 10 && is_vip == 0) {
            openTips('升级到VIP才能购买哦')
        }
    }
    // 购买 礼盒 盲盒 接口
    function buy(id) {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.STOREBUY,
            method: 'POST',
            header: {
                Authorization: token
            },
            data: {
                blind_box_id: id
            },
            success: res => {
                if (res.code == 1) {
                    openTips(res.msg)
                }
                else {
                    if (id == 8) {
                        // location.href = "home.html"
                    }
                    openTips(res.msg)
                };
            }
        });
    }

    // 已获得tab栏
    function tabBar2() {
        for (let i = 0; i < ele.options2.length; i++) {
            ele.options2[i].addEventListener('click', e => {
                let cls = e.target.className;
                typeName2 = cls.split(' ')[0].split('-')[1];
                let typeIndex = typeList2.indexOf(typeName2);
                // console.log(typeIndex, typeName2);
                if (currentPage[typeName2] == 4) {
                    getObtained(typeIndex)
                    // console.log('已购礼盒');
                } else if (currentPage[typeName2] == 5) {
                    // console.log('已购盲盒');
                    getObtained(typeIndex)
                }
            })
        }
    }
    // 获取已获得数据
    function getObtained(type) {
        // console.log(type);
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.OBTAINEDLIST,
            method: 'GET',
            header: {
                Authorization: token
            },
            data: {
                type: type
            },
            success: res => {
                if (res.code == 1) drawPropList2(res.data);
                else openTips(res.msg);
            }
        });
    }
    // 渲染已获得列表
    function drawPropList2(data) {
        // console.log(typeName2);
        let html = '';
        let elm = $('#list-' + typeName2);
        for (let i = 0; i < data.length; i++) {
            let lv = data[i];
            let box = ''
            if (lv.type == 1) {
                let status = lv.status ? 'received' : 'receive';
                let txt = lv.status == 1 ? '已领取' : '领取'
                box = '<div class="giftbox">' +
                    '<div class="giftimg">' +
                    '<img src="http://img.qunx.top/xianwang/lihe/' + lv.blind_box_id + '.png" alt="">' +
                    '</div>' +
                    '<div class="giftinfo">' +
                    '<div class="gifttitle">' +
                    '<span class="a1">' + lv.blind_box.title + '</span>' +
                    '<span class="a2" >剩余：' + lv.surplus_num + '天</span>' +
                    '</div>' +
                    '<div class="gifttime">' +
                    '<div class="giftc">' +
                    '<p class="b1">连续' + lv.blind_box.num + '天每天可领<span class="orgorn">' + lv.blind_box.prize_gift + '米</span>' + '零钱</p>' +
                    '<p class="b1">(合计' + (lv.blind_box.num * lv.blind_box.prize_gift) + '米)</p>' +
                    '</div>' +
                    '<div class="' + status + ' receive-' + lv.id + '" data="' + lv.id + '">' +
                    '<span class="salen">' + txt + '</span>' +
                    '</div>' +
                    '</div>' +
                    '</div>' +
                    '</div>'
            } else {
                // let status = lv.status ? 'openedboxed' : 'openedbox';
                let txt = lv.status == 1 ? '已开盒' : '开盒'
                box = '<div class="giftbox">' +
                    '<div class="giftimg">' +
                    '<img src="http://img.qunx.top/xianwang/maghe/' + (i + 1) + '.png" alt="">' +
                    '</div>' +
                    '<div class="giftinfo">' +
                    '<div class="gifttitle">' +
                    '<span class="a1">' + lv.title + '</span>' +
                    '<span class="a2">剩余：' + lv.surplus_num + '个</span>' +
                    '</div>' +
                    '<div class="gifttime">' +
                    '<div class="giftc">' +
                    '<p class="b1">可随机开出<br>' + '<span class="orgorn">' + parseInt(lv.prize_min) + '-' + parseInt(lv.prize_max) + '米</span>' + '零钱</p>' +
                    '</div>' +
                    '<div class="openedbox" onclick=openedbox(' + lv.blind_box_log_id + ',"' + lv.surplus_num + '")>' +
                    '<span class="salen">' + txt + '</span>' +
                    '</div>' +
                    '</div>' +
                    '</div>' +
                    '</div>'
            }
            html += box
        }
        elm.innerHTML = html;
        bindReceive()
    }


    // 绑定已购礼盒领取按钮
    function bindReceive() {
        let btns = $('.receive');
        for (let i = 0; i < btns.length; i++) {
            btns[i].onclick = function () {
                receiveReward(this.getAttribute('data'));
            }
        }
        let btnds = $('.received');
        for (let i = 0; i < btnds.length; i++) {
            btnds[i].onclick = function () {
                openTips('您已领取过该奖励');
            }
        }
    }
    // 领取已购礼盒奖励
    function receiveReward(lv) {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.STORECOLLECT,
            method: 'POST',
            data: {
                blind_box_log_id: lv,
            },
            header: {
                Authorization: token
            },
            success: res => {
                if (res.code == 1) {
                    successData(res.data)
                    // let btn = $('.receive-' + lv)[0];
                    // btn.innerHTML = "已领取"
                    // btn.classList.remove('receive');
                    // btn.classList.add('received');
                    // btn.onclick = () => {
                    //     openTips('您已领取过该奖励');
                    // };
                    getObtained(1)
                } else {
                    openTips(res.msg);
                }
            }
        })
    }
    // 渲染已获得礼盒弹框
    function successData(data) {
        ele.successMoney.innerHTML = data.prize
        ele.modal_successfully.style.display = 'block'

    }

    //  领取已购盲盒奖励
    window.openedbox = openedbox
    function openedbox(id, num) {
        if (parseInt(num) <= 0) {
            openTips('盲盒数量不足！')
            return
        }
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.STORECOLLECT,
            method: 'POST',
            data: {
                blind_box_log_id: id,
            },
            header: {
                Authorization: token
            },
            success: res => {
                if (res.code == 1) {
                    getObtained(2)
                    ele.openMoney.innerHTML = res.data.prize
                    ele.modal_open.style.display = 'block'
                } else {
                    openTips(res.msg);
                }
            }
        })
    }

})();